﻿using Newtonsoft.Json.Linq;
using SignInServer.Server.Configuration;
using SignInServer.Server.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp1.Project.Configuration;
using WindowsFormsApp1.Project.Info;
using WindowsFormsApp1.Server;
using WindowsFormsApp1.SQL;

namespace SignInServer.Server
{
    /// <summary>
    /// 消息分类处理
    /// </summary>
    class SwitchMessage
    {
        ClientConn clientConn;

        public SwitchMessage(ClientConn clientConn)
        {
            this.clientConn = clientConn;
        }
        public bool switchMessageManager(BasicMessage basicMessage)
        {
            Console.WriteLine("筛选数据：类型" + basicMessage.getMessageType());
            switch (basicMessage.getMessageType())
            {
                case ClientMessageTypeConfiguration.CLIENT_HEARTBEAT:
                    caseHeartBeat(basicMessage); break;
                case ClientMessageTypeConfiguration.CLIENT_MY_LOGIN_INFO:
                    caseMyLoginInfo(basicMessage); break;
                case ClientMessageTypeConfiguration.CLIENT_MY_REGISTER:
                    caseMyRegister(basicMessage); break;
                case ClientMessageTypeConfiguration.CLIENT_USER_GET_INFO_BY_UID:
                    caseUserGetInfoByUID(basicMessage); break;
                case ClientMessageTypeConfiguration.CLIENT_ACTIVITY_REGISTER:
                    caseActivityRegister(basicMessage); break;
                case ClientMessageTypeConfiguration.CLIENT_ACTIVITY_GET_INFO:
                    caseActivityGetInfo(basicMessage); break;
                case ClientMessageTypeConfiguration.CLIENT_MY_JOIN_ACTIVITY:
                    caseActivityJoin(basicMessage); break;
                case ClientMessageTypeConfiguration.CLIENT_ACTIVITY_PARTICIPANT_LIST:
                    caseGetActivityParticipantList(basicMessage);break;
                case ClientMessageTypeConfiguration.CLIENT_ACTIVITY_SIGN_IN:
                    ;break;
                default:caseDefault(basicMessage);break;
            }
            return false;
        }

        private void caseActivityRegister(BasicMessage basicMessage)
        {
            JsonAdaptorForAndroid json = new JsonAdaptorForAndroid(basicMessage.getMainData());
            ActivityInfo activity = new ActivityInfo(json.getInt(MessageNameConfiguration.ACTIVITY_INITIATOR_ID), json.getString(MessageNameConfiguration.ACTIVITY_THEME),
                    json.getDouble(MessageNameConfiguration.ACTIVITY_LONGITUDE), json.getDouble(MessageNameConfiguration.ACTIVITY_LATITUDE),
                    json.getString(MessageNameConfiguration.ACTIVITY_INVITATION_CODE), json.getDateTime(MessageNameConfiguration.ACTIVITY_CHECK_IN_START_TIME),
                    json.getDateTime(MessageNameConfiguration.ACTIVITY_CHECK_IN_END_TIME), json.getDateTime(MessageNameConfiguration.ACTIVITY_START_TIME),
                    json.getDateTime(MessageNameConfiguration.ACTIVITY_END_TIME));
            int activityID=SQLActivityAccess.RegisterActivity(activity).activityID;
            JObject sendJson = new JObject();
            sendJson.Add(MessageNameConfiguration.REGISTER_ACTIVITY_INDEX, json.getInt(MessageNameConfiguration.REGISTER_ACTIVITY_INDEX));
            sendJson.Add(MessageNameConfiguration.ACTIVITY_ID,activityID);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_THEME, json.getString(MessageNameConfiguration.ACTIVITY_THEME));
            clientConn.SendMessage(ServerMessageTypeConfiguration.SERVER_ACTIVITY_REGISTER, Encoding.UTF8.GetBytes(sendJson.ToString()));
        }

        private void caseActivityGetInfo(BasicMessage basicMessage)
        {
            JsonAdaptorForAndroid json = new JsonAdaptorForAndroid(basicMessage.getMainData());
            ActivityInfo activityInfo = SQLActivityAccess.GetActivityInfoByActivityID(json.getInt(MessageNameConfiguration.ACTIVITY_ID));
            JObject sendJson = new JObject();
            sendJson.Add(MessageNameConfiguration.ACTIVITY_ID, activityInfo.activityID);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_INITIATOR_ID, activityInfo.activityInitiatorID);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_THEME, activityInfo.activityTheme);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_LONGITUDE, activityInfo.activityCheckInLongitude);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_LATITUDE, activityInfo.activityCheckInLatitude);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_INVITATION_CODE, activityInfo.activityInvitationCode);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_CHECK_IN_START_TIME, string.Format("{0:g}", activityInfo.activityCheckInStartTime));
            sendJson.Add(MessageNameConfiguration.ACTIVITY_CHECK_IN_END_TIME, string.Format("{0:g}", activityInfo.activityCheckInEndTime));
            sendJson.Add(MessageNameConfiguration.ACTIVITY_START_TIME, string.Format("{0:g}", activityInfo.activityStartTime));
            sendJson.Add(MessageNameConfiguration.ACTIVITY_END_TIME, string.Format("{0:g}", activityInfo.activityEndTime));
            clientConn.SendMessage(ServerMessageTypeConfiguration.SERVER_ACTIVITY_GET_INFO, Encoding.UTF8.GetBytes(sendJson.ToString()));
        }

        private void caseActivityJoin(BasicMessage basicMessage)
        {
            JsonAdaptorForAndroid json = new JsonAdaptorForAndroid(basicMessage.getMainData());
            bool status = SQLActivityAccess.JionInActivity(json.getInt(MessageNameConfiguration.ACTIVITY_ID), 
                json.getInt(MessageNameConfiguration.USER_ID),json.getString(MessageNameConfiguration.ACTIVITY_INVITATION_CODE));
            JObject sendJson = new JObject();
            sendJson.Add(MessageNameConfiguration.ACTIVITY_ID, json.getInt(MessageNameConfiguration.ACTIVITY_ID));
            sendJson.Add(MessageNameConfiguration.ACTIVITY_JOIN_STATUS, status);
            clientConn.SendMessage(ServerMessageTypeConfiguration.SERVER_MY_JOIN_ACTIVITY, Encoding.UTF8.GetBytes(sendJson.ToString()));
        }

        private void caseMyLoginInfo(BasicMessage basicMessage)
        {
            JsonAdaptorForAndroid json = new JsonAdaptorForAndroid(basicMessage.getMainData());
            int uid = json.getInt(MessageNameConfiguration.LOGIN_ID);
            string password = json.getString(MessageNameConfiguration.LOGIN_PASSWORD);
            UserInfo userInfo = SQLUserAccess.UserLoginByID(uid, password);
            JObject sendJson = new JObject();
            if (userInfo!=null)
            {
                sendJson.Add(MessageNameConfiguration.LOGIN_JOINED_ACTIVITY_LIST,
                    ConvertTypeTool.IntListToString(SQLUserAccess.GetJoinedActivityListByUserID(uid).joinedActivityList, '-'));
                sendJson.Add(MessageNameConfiguration.LOGIN_MANAGED_ACTIVITY_LIST,
                    ConvertTypeTool.IntListToString(SQLUserAccess.GetManagedActivityList(uid).managedActivityList, '-'));
                sendJson.Add(MessageNameConfiguration.LOGIN_INITIATOR_ACTIVITY_LIST,
                    ConvertTypeTool.IntListToString(SQLUserAccess.GetInitiatorActivityList(uid).initiatorActivityList, '-'));
                sendJson.Add(MessageNameConfiguration.LOGIN_NAME, userInfo.userName);
                sendJson.Add(MessageNameConfiguration.LOGIN_ID, userInfo.userID);
                sendJson.Add(MessageNameConfiguration.LOGIN_STATUS, true);
            }
            else
            {
                sendJson.Add(MessageNameConfiguration.LOGIN_JOINED_ACTIVITY_LIST,null);
                sendJson.Add(MessageNameConfiguration.LOGIN_MANAGED_ACTIVITY_LIST,null);
                sendJson.Add(MessageNameConfiguration.LOGIN_INITIATOR_ACTIVITY_LIST,null);
                sendJson.Add(MessageNameConfiguration.LOGIN_NAME, null);
                sendJson.Add(MessageNameConfiguration.LOGIN_ID, null);
                sendJson.Add(MessageNameConfiguration.LOGIN_STATUS, false);
            }
            clientConn.SendMessage(ServerMessageTypeConfiguration.SERVER_MY_LOGIN_INFO, Encoding.UTF8.GetBytes(sendJson.ToString()));
        }

        private void caseMyRegister(BasicMessage basicMessage)
        {
            JsonAdaptorForAndroid json = new JsonAdaptorForAndroid(basicMessage.getMainData());
            JObject sendJson = new JObject();
            Console.WriteLine("注册Name:" + json.getString(MessageNameConfiguration.REGISTER_NAME) +
                "注册密码:" + json.getString(MessageNameConfiguration.REGISTER_PASSWORD));
            int uid=SQLUserAccess.RegisterUser(json.getString(MessageNameConfiguration.REGISTER_NAME), 
                json.getString(MessageNameConfiguration.REGISTER_PASSWORD));
            if (uid != -1)
            {
                sendJson.Add(MessageNameConfiguration.REGISTER_STATUS,true);
                sendJson.Add(MessageNameConfiguration.LOGIN_ID, uid);
                sendJson.Add(MessageNameConfiguration.LOGIN_NAME, json.getString(MessageNameConfiguration.REGISTER_NAME));
            }
            else
            {
                sendJson.Add(MessageNameConfiguration.REGISTER_STATUS, false);
                sendJson.Add(MessageNameConfiguration.LOGIN_ID, null);
                sendJson.Add(MessageNameConfiguration.LOGIN_NAME, null);
            }
            clientConn.SendMessage(ServerMessageTypeConfiguration.SERVER_MY_REGISTER, Encoding.UTF8.GetBytes(sendJson.ToString()));
        }

        private void caseUserGetInfoByUID(BasicMessage basicMessage)
        {
            JsonAdaptorForAndroid json = new JsonAdaptorForAndroid(basicMessage.getMainData());
            JObject sendJson = new JObject();
            UserInfo userInfo=SQLUserAccess.GETUserInfoByID(json.getInt(MessageNameConfiguration.USER_ID));
            if (userInfo.userID != -1)
            {
                sendJson.Add(MessageNameConfiguration.LOGIN_JOINED_ACTIVITY_LIST,
                   ConvertTypeTool.IntListToString(SQLUserAccess.GetJoinedActivityListByUserID(userInfo.userID).joinedActivityList, '-'));
                sendJson.Add(MessageNameConfiguration.LOGIN_MANAGED_ACTIVITY_LIST,
                    ConvertTypeTool.IntListToString(SQLUserAccess.GetManagedActivityList(userInfo.userID).managedActivityList, '-'));
                sendJson.Add(MessageNameConfiguration.LOGIN_INITIATOR_ACTIVITY_LIST,
                    ConvertTypeTool.IntListToString(SQLUserAccess.GetInitiatorActivityList(userInfo.userID).initiatorActivityList, '-'));
                sendJson.Add(MessageNameConfiguration.USER_ID, userInfo.userID);
                sendJson.Add(MessageNameConfiguration.USER_NAME, userInfo.userName);
            }
            else
            {
                sendJson.Add(MessageNameConfiguration.LOGIN_JOINED_ACTIVITY_LIST,null);
                sendJson.Add(MessageNameConfiguration.LOGIN_MANAGED_ACTIVITY_LIST,null);
                sendJson.Add(MessageNameConfiguration.LOGIN_INITIATOR_ACTIVITY_LIST,null);
                sendJson.Add(MessageNameConfiguration.USER_ID, userInfo.userID);
                sendJson.Add(MessageNameConfiguration.USER_NAME, null);
            }
            clientConn.SendMessage(ServerMessageTypeConfiguration.SERVER_USER_GET_INFO, Encoding.UTF8.GetBytes(sendJson.ToString()));
        }

        private void caseGetActivityParticipantList(BasicMessage basicMessage)
        {
            JsonAdaptorForAndroid json = new JsonAdaptorForAndroid(basicMessage.getMainData());
            JObject sendJson = new JObject();
            ActivityInfo activityInfo=SQLActivityAccess.GetJoinedActivityListByActivityID(json.getInt(MessageNameConfiguration.ACTIVITY_ID));
            if (activityInfo.activityID != -1)
            {
                List<int> users = new List<int>();
                List<bool> clockIn = new List<bool>();
                List<bool> administrator = new List<bool>();
                for (int i = 0; i < activityInfo.activityParticipant.Count; i++)
                {
                    users.Add(activityInfo.activityParticipant.ElementAt(i).userID);
                    clockIn.Add(activityInfo.activityParticipant.ElementAt(i).clockIn);
                    administrator.Add(activityInfo.activityParticipant.ElementAt(i).administrator);
                }
                sendJson.Add(MessageNameConfiguration.ACTIVITY_PARTICIPANT_USER_ID,
                    ConvertTypeTool.IntListToString(users, '-'));
                sendJson.Add(MessageNameConfiguration.ACTIVITY_PARTICIPANT_CLOCK_IN,
                    ConvertTypeTool.BooleanListToString(clockIn, '-'));
                sendJson.Add(MessageNameConfiguration.ACTIVITY_PARTICIPANT_ADMINISTRATOR,
                    ConvertTypeTool.BooleanListToString(administrator, '-'));
                sendJson.Add(MessageNameConfiguration.ACTIVITY_ID,activityInfo.activityID);
            }
            else
            {
                sendJson.Add(MessageNameConfiguration.ACTIVITY_PARTICIPANT_USER_ID,null);
                sendJson.Add(MessageNameConfiguration.ACTIVITY_PARTICIPANT_CLOCK_IN,null);
                sendJson.Add(MessageNameConfiguration.ACTIVITY_PARTICIPANT_ADMINISTRATOR,null);
                sendJson.Add(MessageNameConfiguration.ACTIVITY_ID, activityInfo.activityID);
            }
            clientConn.SendMessage(ServerMessageTypeConfiguration.SERVER_ACTIVITY_GET_PARTICIPANT_LIST, Encoding.UTF8.GetBytes(sendJson.ToString()));
        }

        private void caseSignInActivity(BasicMessage basicMessage)
        {
            JsonAdaptorForAndroid json = new JsonAdaptorForAndroid(basicMessage.getMainData());
            JObject sendJson = new JObject();
            int activityID = json.getInt(MessageNameConfiguration.ACTIVITY_ID);
            int userID = json.getInt(MessageNameConfiguration.USER_ID);
            bool signInStatus=SQLUserAccess.SignInActivity(activityID, userID);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_SIGN_IN_STATUS, signInStatus);
            sendJson.Add(MessageNameConfiguration.ACTIVITY_ID, activityID);
            sendJson.Add(MessageNameConfiguration.USER_ID, userID);
            clientConn.SendMessage(ServerMessageTypeConfiguration.SERVER_ACTIVITY_SIGN_IN, Encoding.UTF8.GetBytes(sendJson.ToString()));
        }

        private void caseHeartBeat(BasicMessage basicMessage)
        {
            Console.WriteLine("接收到客户端发起的心跳包:" + Encoding.UTF8.GetString(basicMessage.getMainData()));
            String heartbeat = "我在";
            clientConn.SendMessage(ClientMessageTypeConfiguration.CLIENT_HEARTBEAT, Encoding.UTF8.GetBytes(heartbeat));
        }


        private void caseDefault(BasicMessage basicMessage)
        {
            Console.WriteLine("接收到其它不在配置中的数据：" + basicMessage.getMainData());
        }
    }
}
